﻿using CommonBase;
using EnvCommonUtility;
using InteractiveWhiteboardManagement.Base.ViewModels;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace TreeMgtModule.ViewModels
{
    /// <summary>
    /// 类别详情
    /// </summary>
    public class ViewModel_CategoryDetail : WorkspaceViewModel
    {
        #region 变量

        private bool _isNew;
        private Window _infoWindow;
        private DelegateCommand _cmdApply, _cmdCancel;

        #endregion

        #region 构造函数

        /// <summary>
        /// 
        /// </summary>
        /// <param name="infoWindow"></param>
        /// <param name="treeNode">所选结点</param>
        /// <param name="isNew">是否是新增结点</param>
        public ViewModel_CategoryDetail(Window infoWindow,
            ITreeGroup treeNode, bool isNew)
        {
            if (treeNode == null)
                return;

            infoWindow.Closing += (s, e) =>
            {
                if (infoWindow.DialogResult.HasValue && 
                    infoWindow.DialogResult.Value && 
                    WorkingTreeNode != null &&
                    WorkingTreeNode.Arg != null && 
                    WorkingTreeNode.Arg is TagModel && 
                    string.IsNullOrWhiteSpace((WorkingTreeNode.Arg as TagModel).Name))
                {
                    MessageBox.Show("请填写名称！", ConstantString.MSG_COMPANY);
                    e.Cancel = true;
                    return;
                }
            };

            this._infoWindow = infoWindow;
            this._isNew = isNew;
            if (isNew) // 新增结点
            {
                this._workingTreeNode = new TreeGroupModel(string.Empty,
                    new System.Collections.ObjectModel.ObservableCollection<ITreeGroup>())
                    {
                        Arg = new TagModel()
                        {
                            ID = Guid.NewGuid().ToString(),
                            ParentID = (treeNode.Arg as TagModel).ID,
                            Port = "9438",
                            Status = string.Empty,
                        },
                        Parent = treeNode,
                        TooltipText = string.Empty,
                        Level = treeNode == null ? 0 : treeNode.Level + 1, // 0: 根
                        
                    };
            }
            else
            {
                this._workingTreeNode = treeNode;
            }
        }

        #endregion

        #region 属性

        public ITreeGroup _workingTreeNode;
        /// <summary>
        /// 所选树结点
        /// </summary>
        public ITreeGroup WorkingTreeNode
        {
            get
            {
                return _workingTreeNode;
            }
            set
            {
                if (_workingTreeNode == value) return;
                _workingTreeNode = value;
                base.OnPropertyChanged("WorkingTreeNode");
            }
        }


        /// <summary>
        /// 确认
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand Cmd_Apply
        {
            get
            {
                if (_cmdApply == null)
                {
                    _cmdApply = new DelegateCommand(_apply, _canApply);
                }
                return _cmdApply;
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand Cmd_Cancel
        {
            get
            {
                if (_cmdCancel == null)
                {
                    _cmdCancel = new DelegateCommand(_cancel, _canCancel);
                }
                return _cmdCancel;
            }
        }

        #endregion

        #region Private Methods

        #region DelegateCommand

        private bool _canApply()
        {
            return true;
        }

        private bool _canCancel()
        {
            return true;
        }


        /// <summary>
        /// 确认
        /// </summary>
        private void _apply()
        {
            this._infoWindow.DialogResult = true; // 这里会触发 Closed 事件，会自动关闭窗口
        }

        /// <summary>
        /// 取消
        /// </summary>
        private void _cancel()
        {
            this._infoWindow.DialogResult = false;
        }

        #endregion

        #endregion
    }
}
